<?php

namespace app\common\library\wxpay;

use think\contract\Arrayable;

class FzweixinPay
{


    protected $appid;
    protected $mch_id;
    protected $sub_appid;
    protected $sub_mch_id;
    protected $key;
    protected $openid;
    protected $out_trade_no;
    protected $body;
    protected $total_fee;

    function __construct($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $type = 0, $sub_mch_id)
    {
        $this->appid = $appid;
        $this->openid = $openid;
        $this->mch_id = trim($mch_id);
        $this->key = $key;
        $this->out_trade_no = $out_trade_no;
        $this->body = $body;
        $this->total_fee = $total_fee;
        $this->type = $type;
        $this->sub_mch_id = $sub_mch_id;
    }


    public function pay()
    {
        //统一下单接口
        $return = $this->weixinapp();
        return $return;
    }


    //统一下单接口
    private function unifiedorder()
    {

        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        if ($this->type == 1) {//开通会员回调
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/rechargenotify";
        }
        if ($this->type == 2) {//充值回调
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/levelnotify";
        }
        if ($this->type == 3) {//订单支付
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/paynotify";
        }
        if ($this->type == 4) {
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/seatnotify";
        }
        if ($this->type == 5) {//购买商品
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/productnotify";
        }
        if ($this->type == 7) {
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/payrnotify";
        }
        if ($this->type == 8) {
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/Paynotifycoupon";
        }
        if ($this->type == 9) {
            $notify_url = "https://" . $_SERVER['SERVER_NAME'] . "/Paynotifycard";
        }
        $parameters = array(
            'appid' => $this->appid, //小程序ID
            'mch_id' => $this->mch_id, //商户号
            'sub_appid' => $this->appid, //小程序ID
            'sub_mch_id' => $this->sub_mch_id, //子商户商户号
            'nonce_str' => $this->createNoncestr(), //随机字符串
            'body' => $this->body,
            'notify_url' => $notify_url, //通知地址 确保外网能正常访问
            'out_trade_no' => $this->out_trade_no,
            'total_fee' => $this->total_fee,
            'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //终端IP
            'profit_sharing' => 'Y',
            'openid' => $this->openid, //用户id
            'trade_type' => 'JSAPI'//交易类型

        );
        //统一下单签名

        $parameters['sign'] = $this->getSign($parameters);

        $xmlData = $this->arrayToXml($parameters);

        $ret = $this->xmlToArray($this->postXmlCurl($xmlData, $url, 60));

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret;
        } else {
            return "fail";
        }
    }

    //分账接收方
    public function addreceiver($mch_id, $sub_mch_id, $appid, $sub_appid, array $receivers)
    {

        //服务商
        $receivers = json_encode($receivers, JSON_UNESCAPED_UNICODE);
        // 2.生成签名
        $postArr = array(
            'mch_id' => $mch_id,
            'sub_mch_id' => $sub_mch_id,
            'appid' => $appid, //小程序ID
            'sub_appid' => $sub_appid, //小程序ID
            'nonce_str' => md5(time() . rand(1000, 9999)),
            'receiver' => $receivers,
        );
        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求
        //   var_dump($postArr);
        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver';
        $postXML = $wxSign->toXml($postArr);
        $curl_res = $this->request($postXML, $url, false, 2);
        $ret = $wxSign->toArray($curl_res);

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }

    //分账接口
    public function fzorder($mch_id, $sub_mch_id, $appid, $sub_appid, $transaction_id, $out_order_no, array $receivers)
    {

        $receivers = json_encode($receivers, JSON_UNESCAPED_UNICODE);
        //服务商
        // 2.生成签名
        $postArr = array(
            'appid' => $appid,
            'mch_id' => $mch_id,
            'sub_mch_id' => $sub_mch_id,
            'sub_appid' => $sub_appid,
            'nonce_str' => md5(time() . rand(1000, 9999)),
            'transaction_id' => $transaction_id,
            'out_order_no' => $out_order_no,
            'receivers' => $receivers,
        );
        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求
        //  var_dump($postArr);
        $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharing';
        $postXML = $wxSign->toXml($postArr);

        $curl_res = $this->request($postXML, $url, true, 2);
        $ret = $wxSign->toArray($curl_res);
var_dump($ret);
        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }


    //分账退款
    public function refund($mch_id, $sub_mch_id, $appid, $sub_appid, $out_order_no)
    {

        //服务商
        // 2.生成签名
        $postArr = array(
            'appid' => $appid,
            'mch_id' => $mch_id,
            'sub_mch_id' => $sub_mch_id,
            'sub_appid' => $sub_appid,
            'nonce_str' => md5(time() . rand(1000, 9999)),
            'out_trade_no' => $out_order_no,
            'out_refund_no' => $out_order_no,
            'total_fee' => ($this->total_fee),
            'refund_fee' => ($this->total_fee),
        );
        $wxSign = new WxSign();

        $sign = $wxSign->getSign($postArr, 'MD5', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求

        $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
        $postXML = $wxSign->toXml($postArr);
        $curl_res = $this->request($postXML, $url, true, 2);
        $ret = $wxSign->toArray($curl_res);

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return true;
        } else {
            return false;
        }
    }

    //分账结果
    public function fzjg($mch_id, $sub_mch_id, $appid, $sub_appid, $transaction_id, $out_order_no)
    {
        //服务商
        // 2.生成签名
        $postArr = array(
            'mch_id' => $mch_id,
            'sub_mch_id' => $sub_mch_id,
            'transaction_id' => $transaction_id,
            'out_order_no' => $out_order_no,
            'nonce_str' => md5(time() . rand(1000, 9999)),
        );
        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求

        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingquery';
        $postXML = $wxSign->toXml($postArr);

        $curl_res = $this->request($postXML, $url, false, 2);
        $ret = $wxSign->toArray($curl_res);

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }

    //分账结果
    public function jiedong($mch_id, $transaction_id, $out_order_no)
    {
        //服务商
        // 2.生成签名
        $postArr = array(
            'mch_id' => $mch_id,

            'transaction_id' => $transaction_id,
            'out_order_no' => $out_order_no,
            'description' => "分账解冻",
        );
        $wxSign = new WxSign();
        $url = 'https://api.mch.weixin.qq.com/v3/profitsharing/orders/unfreeze';
        //  var_dump($postArr);
        $postXML = $wxSign->toXml($postArr);
        $curl_res = $this->request($postXML, $url, false, 2);

        $ret = $wxSign->toArray($curl_res);

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }

    //分账结果
    public function fzdfz($mch_id, $transaction_id, $out_order_no)
    {
        //服务商
        // 2.生成签名
        $postArr = array(
            'mch_id' => $mch_id,
            'transaction_id' => $transaction_id,
            'nonce_str' => md5(time() . rand(1000, 9999)),
        );
        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求

        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingorderamountquery';
        $postXML = $wxSign->toXml($postArr);

        $curl_res = $this->request($postXML, $url, false, 2);
        $ret = $wxSign->toArray($curl_res);

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }

    //结束分账
    public function endfz($mch_id, $sub_mch_id, $appid, $sub_appid, $transaction_id, $out_order_no)
    {
        // 2.生成签名
        $postArr = array(
            'mch_id' => $mch_id,
            'sub_mch_id' => $sub_mch_id,
            'appid' => $appid,
            'nonce_str' => md5(time() . rand(1000, 9999)),
            'transaction_id' => $transaction_id,
            'out_order_no' => $out_order_no,
            'description' => "分账已完成",
        );

        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求

        $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish';
        $postXML = $wxSign->toXml($postArr);
        $curl_res = $this->request($postXML, $url, true, 2);
        $ret = $wxSign->toArray($curl_res);

        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }


    public function post($url, $data, $timeoutMs = 3000)
    {
        return $this->request($data, $url, $timeoutMs);
    }

    public function request($xml, $url, $useCert = false, $second = 30)
    {


        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);

        //如果有配置代理这里就设置代理
//        if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
//            && WxPayConfig::CURL_PROXY_PORT != 0){
//            curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
//            curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
//        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
            curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        }
        if ($useCert == true) {
            $cert_dir = '';

            if (
                !file_exists($cert_dir . "apiclient_cert.pem") ||
                !file_exists($cert_dir . "apiclient_key.pem")
            ) {
                return "2";
            }

            //设置证书 证书自己参考支付设置
            //使用证书：cert 与 key 分别属于两个.pem文件
            curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLCERT, $cert_dir . "apiclient_cert.pem");
            curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLKEY, $cert_dir . "apiclient_key.pem");
        }
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //运行curl

        $data = curl_exec($ch);

        //返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);

            curl_close($ch);

            return "";
        }
    }

    public function paraFilter($para)
    {
        $paraFilter = array();

        foreach ($para as $key => $val) {
            if ($val == "") {
                continue;

            } else {
                if (!is_array($para[$key])) {
                    $para[$key] = is_bool($para[$key]) ? $para[$key] : trim($para[$key]);
                }

                $paraFilter[$key] = $para[$key];
            }
        }
        return $paraFilter;
    }

    public function arraySort(array $param)
    {
        ksort($param);
        reset($param);
        return $param;
    }

    public function createLinkString($para)
    {
        if (!is_array($para)) {
            throw new \Exception('必须传入数组参数');
        }

        reset($para);
        $arg = "";

        foreach ($para as $key => $val) {

            if (is_array($val)) {
                continue;
            }

            $arg .= $key . "=" . urldecode($val) . "&";
        }

        //去掉最后一个&字符
        $arg = substr($arg, 0, strlen($arg) - 2);

        //如果存在转义字符，那么去掉转义
//        if (get_magic_quotes_gpc()) {
//            $arg = stripslashes($arg);
//        }

        return $arg;
    }

    public function toXml($values)
    {
        if (!is_array($values) || count($values) <= 0) {
            return false;
        }

        $xml = "<xml>";
        foreach ($values as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }

    private static function postXmlCurl($xml, $url, $second = 30)
    {


        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        set_time_limit(0);
        //运行curl
        $data = curl_exec($ch);

        //返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            curl_close($ch);
            //throw new WxPayException("curl出错，错误码:$error");
        }
    }


    //数组转换成xml
    private function arrayToXml($arr)
    {
        $xml = "<root>";
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                $xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            }
        }
        $xml .= "</root>";
        return $xml;
    }


    //xml转换成数组
    private function xmlToArray($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring), true);
        return $val;
    }


    //微信小程序接口
    private function weixinapp()
    {
        //统一下单接口
        $unifiedorder = $this->unifiedorder();


        $time = time();
        $parameters = array(
            'appId' => $this->appid, //小程序ID
            'timeStamp' => "{$time}", //时间戳
            'nonceStr' => $this->createNoncestr(), //随机串
            'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
            'signType' => 'MD5'//签名方式
        );
        //签名
        $parameters['paySign'] = $this->getSign($parameters);
        return $parameters;
    }

    public function fzweixin($mch_id, $sub_mch_id, $appid, $sub_appid, $transaction_id, $out_order_no, $receivers)
    {
        //统一下单接口
        $unifiedorder = $this->fzorder($mch_id, $sub_mch_id, $appid, $sub_appid, $transaction_id, $out_order_no, $receivers);


        $time = time();
        $parameters = array(
            'appId' => $this->appid, //小程序ID
            'timeStamp' => "{$time}", //时间戳
            'nonceStr' => $this->createNoncestr(), //随机串
            'signType' => 'MD5'//签名方式
        );
        //签名
        $parameters['paySign'] = $this->getSign($parameters);
        return $parameters;
    }


    //作用：产生随机字符串，不长于32位
    private function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    public function getSignfz(array $param, $signType = 'MD5', $md5Key)
    {

        $values = $this->paraFilter($param);


        $values = $this->arraySort($values);


        $signStr = $this->createLinkstring($values);


        $signStr .= '&key=' . $md5Key;
        // var_dump($signStr);
        // die;
        switch ($signType) {
            case 'MD5':
                $sign = md5($signStr);
                break;
            case 'HMAC-SHA256':
                $sign = hash_hmac('sha256', $signStr, $md5Key);
                break;
            default:
                $sign = '';
        }

        return strtoupper($sign);
    }


    //作用：生成签名
    private function getSign($Obj)
    {
        foreach ($Obj as $k => $v) {
            $Parameters[$k] = $v;
        }


        //签名步骤一：按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //签名步骤二：在string后加入KEY
        $String = $String . "&key=" . $this->key;
        //签名步骤三：MD5加密
        $String = md5($String);
        //签名步骤四：所有字符转为大写
        $result_ = strtoupper($String);

        return $result_;
    }


    ///作用：格式化参数，签名过程需要使用
    private function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }


    //生成签名
    private function createSign($params)
    {
        //签名步骤一：按字典序排序参数
        ksort($params);
        $String = $this->formatBizQueryParaMapf($params, false);

        //签名步骤二：在string后加入KEY
        $String = $String . "&key=" . $this->key;

        //签名步骤三：加密方式HMAC-SHA256
        // $String = md5($String);
        $String = hash_hmac("sha256", $String, $this->key);
        //签名步骤四：所有字符转为大写
        $result_ = strtoupper($String);

        return $result_;

    }

    //按字典序排序参数
    private function formatBizQueryParaMapf($params, $urlencode = false)
    {
        ksort($params);
        $buff = '';
        foreach ($params as $key => $val) {
            $buff .= $key . "=" . $val . "&";
        }
        $reqPar = substr($buff, 0, strlen($buff) - 1);
        return $reqPar;
    }


    //随机字符串
    private function createNoncestrf($length = 32)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //将数组转换为xml格式
    private function arrayToXmlf($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val))
                $xml .= '<' . $key . '>' . $val . '</' . $key . '>';
            else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }

        $xml .= "</xml>";
        return $xml;
    }


    private function postXmlCurlf($url, $xml, $useCert = false, $second = 30)
    {
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        if ($useCert) {
            //设置证书
            //使用证书：cert 与 key 分别属于两个.pem文件
            //默认格式为PEM，可以注释
            curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLCERT, $this->CertPem);
            //默认格式为PEM，可以注释
            curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLKEY, $this->KeyPem);
        }


        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);


        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        set_time_limit(0);


        //运行curl
        $data = curl_exec($ch);
        //返回结果
        if ($data) {
            curl_close($ch);
            // write_log($data,'wxpay');
            return $data;
        } else {
            // var_dump($data);
            $error = curl_errno($ch);
            write_log("curl出错，错误码:$error", 'wxpay');
            curl_close($ch);
            return false;
        }
    }

    /**
     * 过滤特殊字符
     * @param string $str
     * @return string
     */
    function xmlSafeStr($str)
    {
        return preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $str);
    }

    //分账退款
    public function huitui($mch_id, $sub_mch_id, $appid, $sub_appid, $out_order_no)
    {

        //服务商
        // 2.生成签名
        $postArr = array(
            'appid' => $appid,
            'mch_id' => $mch_id,
            'sub_mch_id' => $sub_mch_id,
            'sub_appid' => $sub_appid,
            'nonce_str' => md5(time() . rand(1000, 9999)),
            'out_order_no' => $out_order_no,
            'out_return_no' => $out_order_no,
            'return_account_type' => 'MERCHANT_ID',
            'return_account' => $mch_id,
            'return_amount' => 1197,
            'description' => '用户退款',
        );
        $wxSign = new WxSign();

        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求

        $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharingreturn';
        $postXML = $wxSign->toXml($postArr);
        $curl_res = $this->request($postXML, $url, true, 2);
        $ret = $wxSign->toArray($curl_res);
//var_dump($ret);
        if (isset($ret['return_code']) && $ret['return_code'] == 'SUCCESS') {
            return true;
        } else {
            return false;
        }
    }

    //分账退款jg
    public function huituijg($mch_id, $sub_mch_id, $appid, $sub_appid, $out_order_no)
    {

        //服务商
        // 2.生成签名
        $postArr = array(
            'appid' => $appid,
            'mch_id' => $sub_mch_id,
            'nonce_str' => md5(time() . rand(1000, 9999)),
            'order_id' => '30002103672023051348727597668',
            'out_return_no' => $out_order_no,

        );
        $wxSign = new WxSign();

        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求

        //   var_dump($postArr);
        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingreturnquery';
        $postXML = $wxSign->toXml($postArr);
        $curl_res = $this->request($postXML, $url, false, 2);
        $ret = $wxSign->toArray($curl_res);
        //   var_dump($ret);exit();
        if (isset($ret['return_code']) && $ret['return_code'] == 'SUCCESS') {
            return true;
        } else {
            return false;
        }
    }

    //分账结果
    public function daifenprice($mch_id, $sub_mch_id, $appid, $sub_appid, $transaction_id, $out_order_no)
    {
        //服务商
        // 2.生成签名
        $postArr = array(
            'mch_id' => $mch_id,
            'transaction_id' => $transaction_id,
            'nonce_str' => md5(time() . rand(1000, 9999)),
        );

        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->key);
        $postArr['sign'] = $sign;
        // 3.发送请求
        //   var_dump($postArr);
        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingorderamountquery';
        $postXML = $wxSign->toXml($postArr);
        //  var_dump($postArr);
        $curl_res = $this->request($postXML, $url, false, 2);
        $ret = $wxSign->toArray($curl_res);
        //    var_dump($ret);exit();
        if (isset($ret['result_code']) && $ret['result_code'] == 'SUCCESS') {
            return $ret['result_code'];
        } else {
            return "fail";
        }
    }

}
